
package com.simplesoftwares.client.library.composite;

import com.google.gwt.editor.client.Editor;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.ResetButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.simplesoftwares.client.library.FlexForm;
import com.simplesoftwares.client.library.FlexForm.FormStyle;
import com.simplesoftwares.client.library.FormField;
import com.simplesoftwares.client.library.FormFieldBuilder;
import com.simplesoftwares.client.library.mywidgets.MyLongBox;
import com.slicktechnologies.shared.common.helperlayer.Address;

/**
 * The Class AddressComposite. Repersents an Adress entity.
 * To Do : On an urgent basis please sets the value to Return Gae type adress.
 */
public  class AddressComposite extends Composite implements HasValue<Address>,CompositeInterface,
	Editor<AddressComposite>
{
	/** The adressline1. */
	public TextBox  adressline1;
	
	/** The adressline2. */
	public TextBox  adressline2;
	
	/** The locality. */
	public TextBox  locality;
	
	/** The land mark. */
	public TextBox  landMark;
	
	/** The country. */
	public ListBox  country;
	
	/** The state. */
	public ListBox  state;
	
	/** The city. */
	public ListBox  city;
	
	/** The pin. */
	public MyLongBox pin;
	
	/** The content. */
	public FlowPanel content;
	
	/** The form. */
	public FlexForm form;
	
	/** The fields. */
	public FormField[][]fields;
	
	/** The builder. */
	public FormFieldBuilder builder;
	
	/** The fpin. */
	public FormField faddr1,faddr2,flocality,flandmark,fcity,fstate,fcountry,fpin;
	
	boolean nonManadatory;
	
	/**
	 * Instantiates a new address composite.
	 */
	public AddressComposite()
	{
		createGui();
		nonManadatory=false;
		initWidget(content);
	}
	
	/**
	 * Initializes the variables necessary for creation of widgets.Change handlers are applied on mandatory widgets.
	 */
	private void initializewidgets()
	{
		adressline1=new TextBox();
		//adressline1.addChangeHandler(this);
		adressline2=new TextBox();
		locality=new TextBox();
		landMark=new TextBox();
		state=new ListBox();
		country=new ListBox();
		city=new ListBox();
		pin=new MyLongBox();
	}
	
	/**
	 * Creates  the Gui through FlexForm Method
	 */
	public void createGui()
	{
		initializewidgets();
		setAdressListBox();
		content= new FlowPanel();
		builder=new FormFieldBuilder("* Address Line 1",adressline1);
		faddr1=builder.setMandatory(true).setMandatoryMsg("Address Line 1 is mandatory!").build();
		builder=new FormFieldBuilder("  Address Line 2",adressline2);
		faddr2=builder.build();
		builder=new FormFieldBuilder("* Locality",locality);
		flocality=builder.setMandatory(true).setMandatoryMsg("Locality is mandatory!").build();
		builder=new FormFieldBuilder("  Landmark",landMark);
		flandmark=builder.build();
		
		builder=  new FormFieldBuilder("* City",city);
		fcity=builder.setMandatory(true).setMandatoryMsg("City is mandatory!").build();
		builder=new FormFieldBuilder("* State",state);
		fstate=builder.setMandatory(true).setMandatoryMsg("State is mandatory!").build();
		builder=new FormFieldBuilder("* Country",country);
		fcountry=builder.setMandatory(true).setMandatoryMsg("Country is mandatory!").build();
		builder=	new FormFieldBuilder("* Pin",pin);
		fpin=builder.setMandatory(true).setMandatoryMsg("Pin is mandatory!").build();
		
		fields=new FormField[][]{
				{faddr1,faddr2,flocality,flandmark},
				{fstate,fcountry,fcity,fpin}
		};
	
		form=new FlexForm(fields,FormStyle.ROWFORM);
		
		applyStyle();
		content.add(form);
	}
	
	
	/* (non-Javadoc)
	 * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
	 */
	@Override
	public HandlerRegistration addValueChangeHandler(
			ValueChangeHandler<Address> handler) {
		
		return null;
	}
	
	/* (non-Javadoc)
	 * @see com.google.gwt.user.client.ui.HasValue#getValue()
	 */
	/**
	 *Returns the composite value which is {@link Address} type object.
	 */
	@Override
	public Address getValue() 
	{
		Address entity= new Address();
			entity.setAddrLine1(this.adressline1.getText().trim());
			entity.setAddrLine2(this.adressline2.getText().trim());
			entity.setLocality(locality.getText().trim());
			entity.setLandmark(this.landMark.getText().trim());
			entity.setState(state.getItemText(this.state.getSelectedIndex()));
			entity.setCity(city.getItemText(city.getSelectedIndex()));
			
			entity.setCountry(this.country.getItemText(this.country.getSelectedIndex()));
			if(pin.getValue()!=null)
			   entity.setPin(pin.getValue());
		
		return entity;
	}
	
	/* (non-Javadoc)
	 * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object)
	 */
	
	/**
	 * Sets the composite with the address entity.
	 */
	@Override
	public void setValue(Address value) 
	{
		adressline1.setText(value.getAddrLine1().trim());
		if(value.getAddrLine2()!=null)
			adressline2.setText(value.getAddrLine2().trim());
		
		
		locality.setText(value.getLocality().trim());
		if(value.getLandmark()!=null)
			landMark.setText(value.getLandmark().trim());
		setList(value.getCity(),city);
		setList(value.getState(),state);
		setList(value.getCountry(),country);
		if(value.getPin()!=0)
		   pin.setValue(value.getPin());
	}
	
	/* (non-Javadoc)
	 * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object, boolean)
	 */
	@Override
	public void setValue(Address value, boolean fireEvents) 
	{
			
	}
	
	/**
	 * Sets the form width.
	 */
	public void applyStyle()
	{
		form.setWidth("98%");
	}
	
	/* (non-Javadoc)
	 * @see com.simplesoftwares.client.library.composite.CompositeInterface#validate()
	 */
	/**
	 * Validates the manditory fields.
	 */
	@Override
	public boolean validate()
	{
		boolean result=true;
		if(nonManadatory==true)
			return true;
		int row=this.fields.length;
		
		for(int k=0;k<row;k++)
		{
			int column=fields[k].length;
			for(int i=0;i<column;i++)
			{
				if(fields[k][i].isMandatory())
				{
					Widget widget=fields[k][i].getWidget();
					if(widget instanceof TextBox)
					{
						TextBox tb=(TextBox) widget;
						String value=tb.getText();
						if(value.equals(""))
						{
							result=false;
							applyBorderColor(widget,"#dd4b39");
							
							InlineLabel l=fields[k][i].getMandatoryMsgWidget();
							if(l!=null)
								l.setVisible(true);
						}
					}
					
					if(widget instanceof MyLongBox)
					{
						MyLongBox tb=(MyLongBox) widget;
						String value=tb.getText();
						if(value.equals(""))
						{
							result=false;
							applyBorderColor(widget,"#dd4b39");
							InlineLabel l=fields[k][i].getMandatoryMsgWidget();
							if(l!=null)
								l.setVisible(true);
						}
					}
					
					if(widget instanceof ListBox)
					{
						ListBox tb=(ListBox) widget;
						if(tb.getSelectedIndex()==0)
						{
							result=false;
							applyBorderColor(widget,"#dd4b39");
							InlineLabel l=fields[k][i].getMandatoryMsgWidget();
							if(l!=null)
								l.setVisible(true);
						}
						
					}
				}
			}
		}
					
		return result;
}
	
	
	/* (non-Javadoc)
	 * @see com.simplesoftwares.client.library.composite.CompositeInterface#clear()
	 */
	/**
	 * Clears the composite.
	 */
	@Override
	public void clear()
	{
		int row=this.fields.length;
		for(int k=0;k<row;k++)
		{
			int colCount =fields[k].length ;
			for(int j=0;j<colCount;j++)
			{
				Widget widg= fields[k][j].getWidget();
				if(widg instanceof TextBox)
				{
					TextBox vb=(TextBox) widg;
					vb.setText("");

					resetBorderColor(widg);
					if(fields[k][j].getMandatoryMsgWidget()!=null)
						fields[k][j].getMandatoryMsgWidget().setVisible(false);
				

				}
				
				if(widg instanceof MyLongBox)
				{
					MyLongBox mb=(MyLongBox) widg;
					mb.setText("");

					resetBorderColor(widg);
					if(fields[k][j].getMandatoryMsgWidget()!=null)
					{
						fields[k][j].getMandatoryMsgWidget().setVisible(false);
					    
					}

					resetBorderColor(widg);

				}
				
				if(widg instanceof ListBox)
				{
					ListBox lb=(ListBox) widg;
					lb.setSelectedIndex(0);
					resetBorderColor(widg);
					if(fields[k][j].getMandatoryMsgWidget()!=null)
						fields[k][j].getMandatoryMsgWidget().setVisible(false);


				}
				
			}
			
			
		}
	}
				
 /**
  * Applies background color.Called when validation get failed
  *
  * @param widg the widg
  * @param color the color
  */
 public void applyBorderColor(Widget widg,String color)
 {
	 widg.getElement().getStyle().setBorderColor(color);
	 
 }
 
	

@Override
public void setEnable(boolean status)
{
	int row=this.fields.length;
	for(int k=0;k<row;k++)
	{
		int colCount =fields[k].length ;
		for(int j=0;j<colCount;j++)
		{
			Widget widg= fields[k][j].getWidget();
			if(widg instanceof TextBox)
			{
				TextBox vb=(TextBox) widg;
				vb.setEnabled(status);
			}
			
			if(widg instanceof MyLongBox)
			{
				MyLongBox mb=(MyLongBox) widg;
				mb.setEnabled(status);
			}
			
			if(widg instanceof ListBox)
			{
				ListBox lb=(ListBox) widg;
				lb.setEnabled(status);
			}
		}
	}
}
///////////////////////////////////////To Be expanded in near future////////////////////////////////////////////////////
/**
 * Method sets the hard coded values in city ,country,state list boxes.
 * To Do On an urgent basis integrate dynamic behavior such as in any common web application.
 */
public void setAdressListBox()
{
	this.city.addItem("--SELECT--");
	this.city.addItem("Pune");
	this.city.addItem("Mumbai");
	this.city.addItem("Thane");
	
	
	this.country.addItem("--SELECT--");
	this.country.addItem("India");
	
	this.state.addItem("--SELECT--");
	this.state.addItem("Maharashtra");

}

public void setList(String value,ListBox lb)
{
	for(int i=0;i<lb.getItemCount();i++)
	{
		//System.out.println("INSIDE ADRESS LIST BOX "+value.trim().equals(lb.getItemText(lb.getSelectedIndex())));
		if(value.trim().equals(lb.getItemText(i)))
		{
			lb.setSelectedIndex(i);
		}
	}
}

private void resetBorderColor(Widget widg)
{
	 widg.getElement().getStyle().clearBorderColor();
}

public TextBox getAdressline1() {
	return adressline1;
}

public void setAdressline1(TextBox adressline1) {
	this.adressline1 = adressline1;
}

public TextBox getAdressline2() {
	return adressline2;
}

public void setAdressline2(TextBox adressline2) {
	this.adressline2 = adressline2;
}

public TextBox getLocality() {
	return locality;
}

public void setLocality(TextBox locality) {
	this.locality = locality;
}

public TextBox getLandMark() {
	return landMark;
}

public void setLandMark(TextBox landMark) {
	this.landMark = landMark;
}

public ListBox getCountry() {
	return country;
}

public void setCountry(ListBox country) {
	this.country = country;
}

public ListBox getState() {
	return state;
}

public void setState(ListBox state) {
	this.state = state;
}

public ListBox getCity() {
	return city;
}

public void setCity(ListBox city) {
	this.city = city;
}

public MyLongBox getPin() {
	return pin;
}

public void setPin(MyLongBox pin) {
	this.pin = pin;
}

public FlowPanel getContent() {
	return content;
}

public void setContent(FlowPanel content) {
	this.content = content;
}

public FlexForm getForm() {
	return form;
}

public void setForm(FlexForm form) {
	this.form = form;
}

public FormField[][] getFields() {
	return fields;
}

public void setFields(FormField[][] fields) {
	this.fields = fields;
}

public FormFieldBuilder getBuilder() {
	return builder;
}

public void setBuilder(FormFieldBuilder builder) {
	this.builder = builder;
}

public FormField getFaddr1() {
	return faddr1;
}

public void setFaddr1(FormField faddr1) {
	this.faddr1 = faddr1;
}

public FormField getFaddr2() {
	return faddr2;
}

public void setFaddr2(FormField faddr2) {
	this.faddr2 = faddr2;
}

public FormField getFlocality() {
	return flocality;
}

public void setFlocality(FormField flocality) {
	this.flocality = flocality;
}

public FormField getFlandmark() {
	return flandmark;
}

public void setFlandmark(FormField flandmark) {
	this.flandmark = flandmark;
}

public FormField getFcity() {
	return fcity;
}

public void setFcity(FormField fcity) {
	this.fcity = fcity;
}

public FormField getFstate() {
	return fstate;
}

public void setFstate(FormField fstate) {
	this.fstate = fstate;
}

public FormField getFcountry() {
	return fcountry;
}

public void setFcountry(FormField fcountry) {
	this.fcountry = fcountry;
}

public FormField getFpin() {
	return fpin;
}

public void setFpin(FormField fpin) {
	this.fpin = fpin;
}

public boolean isNonManadatory() {
	return nonManadatory;
}

public void setNonManadatory(boolean nonManadatory) {
	this.nonManadatory = nonManadatory;
	if(nonManadatory==true)
	{
		faddr1.getHeaderLabel().setText("Address Line 1 ");
		flocality.getHeaderLabel().setText("Locality");
		fcity.getHeaderLabel().setText("City");
		fpin.getHeaderLabel().setText("State");
		fcountry.getHeaderLabel().setText("Country");
		fstate.getHeaderLabel().setText("State");
	}
}



}
